<?php
class Project_m extends MY_Model{
	
	function __construct(){
		parent::__construct();
		$this->_table = 'business_project';
	}
	
	public function get_all($param = array()){
		//date_default_timezone_set('Asia/Ho_Chi_Minh');
		//lay cong viec trong 1 thang
		//
		//$this->db->where('parent_id',0);
		if(!empty($param['month_start'])){
			//print_r($param['month_start']);exit;
			$start = strtotime($param['month_start']);
			//echo $start;exit;
			$end = strtotime(date("Y-m", $start) . " +1 month");
			$this->db->where('date_start >= ',$start);
			$this->db->where('date_start <= ',$end);
			//echo date('Y-m',$start).'<br />';
			//echo date('Y-m',$end).'<br />';
		}
		//cong viec cua phong ban
		if(!empty($param['room'])){
			$this->db->where('room_id',$param['room']);
		}
		if(!empty($param['room_id2'])){
			$this->db->where('room_id2',$param['room_id2']);
		}
		//parent::get_all();
		//echo $this->db->last_query();exit;
		return parent::get_all();
	}
	
	public function get_many_by($param = array(),$page_size = 10,$join_table = TRUE){
		$this->get_many_by_query($param,$page_size,$join_table);
		$total_row = $this->db->count_all_results($this->_table);//$this->get_total_row();
		
		$this->get_many_by_query($param,$page_size,$join_table);
		if ( ! empty($param['limit']))
		{
			 $this->db->limit($page_size,$param['limit'] * $page_size);			
		}
		else
			$this->db->limit($page_size);			
		$projects = parent::get_all();
		$this->load->model('project_users_m');
		foreach($projects as $p){
			$p->users = $this->project_users_m->get_all(array('id'=>$p->id));
		}
		//echo $this->db->last_query();
		//$total_row = $this->db->count_all_results();//$this->get_total_row();
		$result = new stdClass();
		$result->projects = $projects;
		$result->total_row = $total_row;
		return $result;
	}
	
	private function get_many_by_query($param = array(),$page_size = 10,$join_table = TRUE){
		$this->_table = 'business_project p';
		$this->db->select('p.*,a.display_name');
		$this->db->join('ser_users a','a.id = p.user_receive');
		if(!empty($param['parent_id']))
			$this->db->where('p.parent_id',$param['parent_id']);
		//giam doc xem parent id = 0 va khi vao chi tiet se co cong viec lien quan
		else{
			if($this->current_user->group_id == 2)
				$this->db->where('p.parent_id',0);
		}
		if(!empty($join_table)){
			$this->db->join('business_project_users u','u.id = p.id');
			//$this->db->where('u.important',1);
		}
		if(!empty($param['user_id'])){
			$this->db->where('u.user_id',$param['user_id']);
		}
		if(isset($param['status'])){
			$this->db->where('p.status',$param['status']);
		}
		if(!empty($param['doing'])){ // Dang thuc hien			
			$this->db->where('p.date_end >= '.time());
		}		
		if(!empty($param['finish_good'])){ // hoan thanh dung tien do controller type3
			$this->db->where('p.date_finish <= p.date_end');
		}
		if(!empty($param['finish_bad'])){ // hoan thanh tre tien do controller type4
			$time = strtotime(date("Y-m-d", time()));
			//$this->db->where('p.date_finish > p.date_end');
			$this->db->where('p.status = 0');
			$this->db->where('p.date_end < '. $time);
		}
		if(!empty($param['finish_bad_2'])){ // hoan thanh tre tien do controller type4
			//$this->db->where('p.date_finish > p.date_end');
			$this->db->where('p.status = 1');
			$this->db->where('p.date_end < p.date_finish');			
		}
		if(!empty($param['room_id'])){
			$this->db->where('p.room_id',$param['room_id']);
		}
		if(!empty($param['room_id2'])){ // where in
			$this->db->where_in('p.room_id2',$param['room_id2']);
		}
		if(!empty($param['f_employee'])){ // select
			$this->db->where('p.user_receive',$param['f_employee']);
		}
		if(!empty($param['f_priority'])){ // select
			$this->db->where('p.priority_id',$param['f_priority']);
		}
		if(!empty($param['finish_bad_2'])){ // type5
			$time = strtotime(date("Y-m-d", time()));
			$this->db->where('(p.date_finish > p.date_end OR (p.status=1 AND p.date_end < '. $time .')) ');
		}
		
		
		//theo thoi gian
		//print_r($param);
		if(!empty($param['f_date_status'])){
			$one = 86400;//1 ngay
			$curr = strtotime(date("Y-m-d  H:i"));
			//echo $curr;exit;
			if($param['f_date_status'] == 'date_first'){ // sap bat dau 
				$this->db->where('p.date_start >= \''.$curr.'\'');
			}
			if($param['f_date_status'] == 'date_last'){ // sap ket thuc 3 ngay + phai lon hon ngay hien tai
				$e = ($curr) + (3 * $one);
				//echo $e;exit;
				$this->db->where('p.date_end > \''. $curr .'\'');
				$this->db->where('p.date_end <= \''. $e .'\'');
			}
			if($param['f_date_status'] == 'date_last_day'){//het han trong ngay
				$start = strtotime(date("Y-m-d")); //00:00
				$end = $curr + $one;//+1 day
				$this->db->where('p.date_end >= \''. $start .'\'');
				$this->db->where('p.date_end < \''. $end .'\'');
			}
		}
		$this->db->order_by('p.status');
		$this->db->order_by('p.percent');
		$this->db->order_by('p.priority_id','desc');
		$this->db->order_by('p.date_start','desc');
		
	}
	
	public function get($id){
		$result = new stdClass();
		$project = parent::get($id);
		$this->_table = 'business_project_comment';
		$this->db->where('project_id',$id);
		$comments = parent::get_all();
		$result->project = $project;
		$result->comments = $comments;
		return $result;
	}
	
	public function update($id,$data){
		$this->_table = 'business_project';
		parent::update($id,$data);
	}
	
	public function delete($id){
		$this->_table = 'business_project';
		
		parent::delete($id);
		$this->db->where('parent_id',$id);
		$list = parent::get_all();
		
		$this->load->model('project_users_m');
		$this->project_users_m->delete_by_key($id);
		//xoa child
		
		foreach($list as $item){
			$this->delete($item->id);
		}
	}
}